//
//  HomeViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/5.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "HomeViewController.h"

#import "SousuoViewController.h"
#import "Right_BtnViewController.h"
#import "HomeProductCollectionViewCell.h"
#import "ChanpinliebiaoViewController.h"
#import "ChanpinxiangqingViewController.h"

@interface HomeViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

//top
@property (nonatomic, strong) UIView * view_topSearch;
@property (nonatomic, strong) UIImageView * image_search;
@property (nonatomic, strong) UILabel * label_search;
@property (nonatomic, strong) UIImageView * image_erji;

//tableView
@property (nonatomic, strong) UITableView * tableView;

//轮播图
@property (nonatomic, strong) UIScrollView * lunbo_scrollView;
@property (nonatomic, strong) UIPageControl * lunbo_pageControl;
@property (nonatomic, strong) NSTimer * lunbo_timer;
@property (nonatomic, assign) BOOL isplay;
//轮播展示图片
@property (nonatomic, strong) UIImageView * image1;
@property (nonatomic, strong) UIImageView * image2;

//限时购
@property (nonatomic, strong) UIImageView * image_jianggou;
@property (nonatomic, strong) UILabel * time_1;
@property (nonatomic, strong) UILabel * time_2;
@property (nonatomic, strong) UILabel * time_3;
@property (nonatomic, strong) UIImageView * image_timedian1;
@property (nonatomic, strong) UIImageView * image_timedian2;
@property (nonatomic, strong) NSTimer * timer;

//推荐
@property (nonatomic, strong) UIView * view_tuijian1;
@property (nonatomic, strong) UIView * view_tuijian2;
@property (nonatomic, strong) UIView * view_tuijian3;

//猜你喜欢
@property (nonatomic, strong) UICollectionView * collectionView_product;


//请求数据
@property (nonatomic, strong) NSMutableArray * arr_lunboData;
@property (nonatomic, strong) NSMutableArray * arr_tuijianData;
@property (nonatomic, strong) NSMutableArray * arr_cainixihuanData;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _lblTitle.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.hidden = YES;
    
    //
    self.view_topSearch = [[UIView alloc] initWithFrame:CGRectMake(10, 20 + 4.5, SCREEN_WIDTH - 10 - 50, 35)];
    self.view_topSearch.backgroundColor = [UIColor whiteColor];
    self.view_topSearch.alpha = 0.7;
    self.view_topSearch.layer.cornerRadius = 3;
    [self.view addSubview:self.view_topSearch];
    
    UIButton * btn_topSearch = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_topSearch.frame = CGRectMake(0, 0, SCREEN_WIDTH - 60, 35);
    [self.view_topSearch addSubview:btn_topSearch];
    [btn_topSearch addTarget:self action:@selector(btn_topSearchAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.image_search = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20 + 12, 20, 20)];
    self.image_search.image = [UIImage imageNamed:@"sousuo@2x.png"];
    [self.view addSubview:self.image_search];
    
    self.label_search = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.image_search.frame) + 5, 20 + 14.5, SCREEN_WIDTH - CGRectGetMaxX(self.image_search.frame) - 5 - 50, 15)];
    self.label_search.text = @"特价水果限量抢购!超值实惠不容错过";
    self.label_search.textColor = [UIColor grayColor];
    self.label_search.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.label_search];
    
    //
    self.image_erji = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 40 + 2.5, 20 + 9.5, 25, 25)];
    self.image_erji.userInteractionEnabled = YES;
    self.image_erji.image = [UIImage imageNamed:@"kefu@2x.png"];
    [self.view addSubview:self.image_erji];
    
    UIButton * btn_image_erji = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_image_erji.frame = CGRectMake(0, 0, 30, 30);
    [self.image_erji addSubview:btn_image_erji];
    [btn_image_erji addTarget:self action:@selector(btn_image_erjiAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//搜索栏
- (void )btn_topSearchAction:(UIButton *)sender
{
//    NSLog(@"跳转搜索页面");
    
    SousuoViewController * sousuoViewController = [[SousuoViewController alloc] init];
    [self showViewController:sousuoViewController sender:nil];
}

//右边栏
- (void )btn_image_erjiAction:(UIButton *)sender
{
//    NSLog(@"右边栏");
    
//    Right_BtnViewController * right_BtnViewController = [[Right_BtnViewController alloc] init];
//    [self showViewController:right_BtnViewController sender:nil];
    
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    //
    [self.view bringSubviewToFront:self.view_topSearch];
    [self.view bringSubviewToFront:self.image_erji];
    [self.view bringSubviewToFront:self.image_search];
    [self.view bringSubviewToFront:self.label_search];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        if(SCREEN_WIDTH == 375)
        {
            return 180;
        }
        else if(SCREEN_WIDTH == 320)
        {
            return 150;
        }
        else
        {
            return 200;
        }
    }
    else if(indexPath.row == 1)
    {
        CGFloat length_x = (SCREEN_WIDTH - 130) / 4;

        return (length_x + 25) * 2 + 40;
    }
    else if(indexPath.row == 2)
    {
        if(SCREEN_WIDTH == 375)
        {
            return 180 + 50;
        }
        else if(SCREEN_WIDTH == 320)
        {
            return 150 + 50;
        }
        else
        {
            return 200 + 50;
        }
        
//        return 40;
    }
    else if(indexPath.row == 3)
    {
        CGFloat length_x = (SCREEN_WIDTH - 40) / 3;
        
        return length_x + 55;
    }
    else if(indexPath.row == 4)
    {
        return 50;
    }
    else
    {
        int item_length = (SCREEN_WIDTH - 30) / 2;

        self.arr_cainixihuanData = @[@""].mutableCopy;
        
        if(self.arr_cainixihuanData.count % 2 == 0)
        {
           return (self.arr_cainixihuanData.count / 2) * (item_length + 55 + 10) + 10;
        }
        else
        {
            return ((self.arr_cainixihuanData.count + 1) / 2) * (item_length + 55 + 10) + 10;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    if(indexPath.row == 0)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self p_lunbotu];
        
        [cell addSubview:self.lunbo_scrollView];
        [cell addSubview:self.lunbo_pageControl];
    }
    else if(indexPath.row == 1)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        CGFloat length_x = (SCREEN_WIDTH - 130) / 4;
        
        //1
        UIImageView * image_1 = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, length_x, length_x)];
        image_1.layer.cornerRadius = length_x / 2;
        image_1.backgroundColor = [UIColor orangeColor];
        image_1.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_1];
        
        UILabel * name_1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_1.frame) - 10, CGRectGetMaxY(image_1.frame) + 5, length_x + 20, 20)];
        name_1.font = [UIFont systemFontOfSize:14];
        name_1.textAlignment = NSTextAlignmentCenter;
        name_1.text = @"水果";
        [cell addSubview:name_1];
        
        UIButton * btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_1.frame = CGRectMake(CGRectGetMinX(image_1.frame) - 10, CGRectGetMinY(image_1.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_1];
        btn_1.tag = 151;
        [btn_1 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];

        //2
        UIImageView * image_2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_1.frame) + 30, 10, length_x, length_x)];
        image_2.layer.cornerRadius = length_x / 2;
        image_2.backgroundColor = [UIColor orangeColor];
        image_2.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_2];
        
        UILabel * name_2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_2.frame) - 10, CGRectGetMaxY(image_2.frame) + 5, length_x + 20, 20)];
        name_2.font = [UIFont systemFontOfSize:14];
        name_2.textAlignment = NSTextAlignmentCenter;
        name_2.text = @"水果";
        [cell addSubview:name_2];
        
        UIButton * btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_2.frame = CGRectMake(CGRectGetMinX(image_2.frame) - 10, CGRectGetMinY(image_2.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_2];
        btn_2.tag = 152;
        [btn_2 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //3
        UIImageView * image_3 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_2.frame) + 30, 10, length_x, length_x)];
        image_3.layer.cornerRadius = length_x / 2;
        image_3.backgroundColor = [UIColor orangeColor];
        image_3.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_3];
        
        UILabel * name_3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_3.frame) - 10, CGRectGetMaxY(image_3.frame) + 5, length_x + 20, 20)];
        name_3.font = [UIFont systemFontOfSize:14];
        name_3.textAlignment = NSTextAlignmentCenter;
        name_3.text = @"水果";
        [cell addSubview:name_3];
        
        UIButton * btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_3.frame = CGRectMake(CGRectGetMinX(image_3.frame) - 10, CGRectGetMinY(image_3.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_3];
        btn_3.tag = 153;
        [btn_3 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //4
        UIImageView * image_4 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_3.frame) + 30, 10, length_x, length_x)];
        image_4.layer.cornerRadius = length_x / 2;
        image_4.backgroundColor = [UIColor orangeColor];
        image_4.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_4];
        
        UILabel * name_4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_4.frame) - 10, CGRectGetMaxY(image_4.frame) + 5, length_x + 20, 20)];
        name_4.font = [UIFont systemFontOfSize:14];
        name_4.textAlignment = NSTextAlignmentCenter;
        name_4.text = @"水果";
        [cell addSubview:name_4];
        
        UIButton * btn_4 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_4.frame = CGRectMake(CGRectGetMinX(image_4.frame) - 10, CGRectGetMinY(image_4.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_4];
        btn_4.tag = 154;
        [btn_4 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //5
        UIImageView * image_5 = [[UIImageView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(btn_1.frame) + 10, length_x, length_x)];
        image_5.layer.cornerRadius = length_x / 2;
        image_5.backgroundColor = [UIColor orangeColor];
        image_5.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_5];
        
        UILabel * name_5 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_5.frame) - 10, CGRectGetMaxY(image_5.frame) + 5, length_x + 20, 20)];
        name_5.font = [UIFont systemFontOfSize:14];
        name_5.textAlignment = NSTextAlignmentCenter;
        name_5.text = @"水果";
        [cell addSubview:name_5];
        
        UIButton * btn_5 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_5.frame = CGRectMake(CGRectGetMinX(image_5.frame) - 10, CGRectGetMinY(image_5.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_5];
        btn_5.tag = 155;
        [btn_5 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //6
        UIImageView * image_6 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_5.frame) + 30,CGRectGetMinY(image_5.frame), length_x, length_x)];
        image_6.layer.cornerRadius = length_x / 2;
        image_6.backgroundColor = [UIColor orangeColor];
        image_6.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_6];
        
        UILabel * name_6 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_6.frame) - 10, CGRectGetMaxY(image_6.frame) + 5, length_x + 20, 20)];
        name_6.font = [UIFont systemFontOfSize:14];
        name_6.textAlignment = NSTextAlignmentCenter;
        name_6.text = @"水果";
        [cell addSubview:name_6];
        
        UIButton * btn_6 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_6.frame = CGRectMake(CGRectGetMinX(image_6.frame) - 10, CGRectGetMinY(image_6.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_6];
        btn_6.tag = 156;
        [btn_6 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //7
        UIImageView * image_7 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_6.frame) + 30,CGRectGetMinY(image_6.frame), length_x, length_x)];
        image_7.layer.cornerRadius = length_x / 2;
        image_7.backgroundColor = [UIColor orangeColor];
        image_7.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_7];
        
        UILabel * name_7 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_7.frame) - 10, CGRectGetMaxY(image_7.frame) + 5, length_x + 20, 20)];
        name_7.font = [UIFont systemFontOfSize:14];
        name_7.textAlignment = NSTextAlignmentCenter;
        name_7.text = @"水果";
        [cell addSubview:name_7];
        
        UIButton * btn_7 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_7.frame = CGRectMake(CGRectGetMinX(image_7.frame) - 10, CGRectGetMinY(image_7.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_7];
        btn_7.tag = 157;
        [btn_7 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //8
        UIImageView * image_8 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_7.frame) + 30,CGRectGetMinY(image_7.frame), length_x, length_x)];
        image_8.layer.cornerRadius = length_x / 2;
        image_8.backgroundColor = [UIColor orangeColor];
        image_8.image = [UIImage imageNamed:@"shuiguoshengxian@2x.png"];
        [cell addSubview:image_8];
        
        UILabel * name_8 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(image_8.frame) - 10, CGRectGetMaxY(image_8.frame) + 5, length_x + 20, 20)];
        name_8.font = [UIFont systemFontOfSize:14];
        name_8.textAlignment = NSTextAlignmentCenter;
        name_8.text = @"水果";
        [cell addSubview:name_8];
        
        UIButton * btn_8 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_8.frame = CGRectMake(CGRectGetMinX(image_8.frame) - 10, CGRectGetMinY(image_8.frame), length_x + 20, length_x + 25);
        [cell addSubview:btn_8];
        btn_8.tag = 158;
        [btn_8 addTarget:self action:@selector(btn_fenleiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //分界线
        UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, (length_x + 25) * 2 + 30, SCREEN_WIDTH, 10)];
        view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line];
    }
    else if(indexPath.row == 2)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        self.image_jianggou = [[UIImageView alloc] init];
        if(SCREEN_WIDTH == 375)
        {
            self.image_jianggou.frame = CGRectMake(0, 0, SCREEN_WIDTH, 180);
        }
        else if(SCREEN_WIDTH == 320)
        {
            self.image_jianggou.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
        }
        else
        {
            self.image_jianggou.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
        }
        self.image_jianggou.userInteractionEnabled = YES;
        self.image_jianggou.image = [UIImage imageNamed:@"xianshiqiang@2x.png"];
        [cell addSubview:self.image_jianggou];
        
        UIButton * btn_jianggou = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_jianggou.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.image_jianggou.frame.size.height);
        [cell addSubview:btn_jianggou];
        [btn_jianggou addTarget:self action:@selector(btn_jianggouAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.image_jianggou.frame), SCREEN_WIDTH, 10)];
        view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line];
        
        UIImageView * image_tuijian = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, CGRectGetMaxY(view_line.frame) + 7.5, 25, 25)];
        image_tuijian.image = [UIImage imageNamed:@"tuijian@2x.png"];
        [cell addSubview:image_tuijian];
        
        UILabel * label_tuijian = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_tuijian.frame) + 5, CGRectGetMaxY(view_line.frame) + 10, 200, 20)];
        label_tuijian.text = @"新品推荐";
        label_tuijian.textColor = [UIColor orangeColor];
        label_tuijian.font = [UIFont systemFontOfSize:17];
        [cell addSubview:label_tuijian];

        //倒计时
        NSDate * date1 = [NSDate date];
        NSDateFormatter * format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        //当前时间
        NSString * str_1 = [format stringFromDate:date1];
        NSString * str_date = [str_1 substringToIndex:10];
        //最终时间
        NSString * str_2 = [NSString stringWithFormat:@"%@ 23:59:59",str_date];
        NSDate * date2 = [format dateFromString:str_2];
        NSTimeInterval x = [date2 timeIntervalSinceDate:date1];
        
        int t_1 = (x + 1) / 3600;
        int t_2 = ((x + 1) - t_1 * 3600) / 60;
        int t_3 = ((x + 1) - t_1 * 3600 - t_2 * 60);
        
        self.time_3 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 10 - 25, 10, 25, 25)];
        self.time_3.backgroundColor = [UIColor blackColor];
        self.time_3.textColor = [UIColor whiteColor];
        self.time_3.textAlignment = NSTextAlignmentCenter;
        self.time_3.text = [NSString stringWithFormat:@"%02d",t_3];
        [cell addSubview:self.time_3];
        
        self.time_2 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 35 - 40, 10, 25, 25)];
        self.time_2.backgroundColor = [UIColor blackColor];
        self.time_2.textColor = [UIColor whiteColor];
        self.time_2.textAlignment = NSTextAlignmentCenter;
        self.time_2.text = [NSString stringWithFormat:@"%02d",t_2];
        [cell addSubview:self.time_2];
        
        self.time_1 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 35 - 40 - 40, 10, 25, 25)];
        self.time_1.backgroundColor = [UIColor blackColor];
        self.time_1.textColor = [UIColor whiteColor];
        self.time_1.textAlignment = NSTextAlignmentCenter;
        self.time_1.text = [NSString stringWithFormat:@"%02d",t_1];
        [cell addSubview:self.time_1];
        
        //
        self.image_timedian1 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.time_1.frame) + 6, 17, 3, 12)];
        self.image_timedian1.image = [UIImage imageNamed:@"timedian@2x.png"];
        [cell addSubview:self.image_timedian1];
        self.image_timedian2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.time_2.frame) + 6, 17, 3, 12)];
        self.image_timedian2.image = [UIImage imageNamed:@"timedian@2x.png"];
        [cell addSubview:self.image_timedian2];
        
        //倒计时
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(qianggou_timerAction) userInfo:nil repeats:YES];
        
//        UIImageView * image_tuijian = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, 7.5, 25, 25)];
//        image_tuijian.image = [UIImage imageNamed:@"tuijian@2x.png"];
//        [cell addSubview:image_tuijian];
//        
//        UILabel * label_tuijian = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_tuijian.frame) + 5, 10, 200, 20)];
//        label_tuijian.text = @"新品推荐";
//        label_tuijian.textColor = [UIColor orangeColor];
//        label_tuijian.font = [UIFont systemFontOfSize:17];
//        [cell addSubview:label_tuijian];
    }
    else if(indexPath.row == 3)
    {
        self.arr_tuijianData = @[@"",@""].mutableCopy;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        CGFloat length_x = (SCREEN_WIDTH - 40) / 3;
        
        //1
        self.view_tuijian1 = [[UIView alloc] initWithFrame:CGRectMake(10, 5, length_x, length_x + 45)];
        self.view_tuijian1.backgroundColor = [UIColor whiteColor];
        [cell addSubview:self.view_tuijian1];
        
        UIImageView * image_1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, length_x, length_x)];
        [image_1 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"tuijianchanpin@2x.png"]];
        [self.view_tuijian1 addSubview:image_1];
        
        UILabel * name_1 = [[UILabel alloc] initWithFrame:CGRectMake(7, CGRectGetMaxY(image_1.frame) + 7, length_x - 8, 15)];
        name_1.text = @"产品名称_1";
        name_1.font = [UIFont systemFontOfSize:15];
        [self.view_tuijian1 addSubview:name_1];
        
        UILabel * price_1 = [[UILabel alloc] initWithFrame:CGRectMake(6, CGRectGetMaxY(name_1.frame) + 5, length_x - 6, 15)];
        price_1.text = @"￥30.0";
        price_1.textColor = [UIColor redColor];
        price_1.font = [UIFont systemFontOfSize:15];
        [self.view_tuijian1 addSubview:price_1];
        
        UIButton * btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_1.frame = CGRectMake(0, 0, length_x, length_x + 45);
        [self.view_tuijian1 addSubview:btn_1];
        btn_1.tag = 251;
        [btn_1 addTarget:self action:@selector(btn_tuijianAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //2
        self.view_tuijian2 = [[UIView alloc] initWithFrame:CGRectMake(20 + length_x, 5, length_x, length_x + 45)];
        self.view_tuijian2.backgroundColor = [UIColor whiteColor];
        [cell addSubview:self.view_tuijian2];
        
        UIImageView * image_2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, length_x, length_x)];
        [image_2 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"tuijianchanpin@2x.png"]];
        [self.view_tuijian2 addSubview:image_2];
        
        UILabel * name_2 = [[UILabel alloc] initWithFrame:CGRectMake(7, CGRectGetMaxY(image_2.frame) + 7, length_x - 8, 15)];
        name_2.text = @"产品名称_2";
        name_2.font = [UIFont systemFontOfSize:15];
        [self.view_tuijian2 addSubview:name_2];
        
        UILabel * price_2 = [[UILabel alloc] initWithFrame:CGRectMake(6, CGRectGetMaxY(name_2.frame) + 5, length_x - 6, 15)];
        price_2.text = @"￥20.9";
        price_2.textColor = [UIColor redColor];
        price_2.font = [UIFont systemFontOfSize:15];
        [self.view_tuijian2 addSubview:price_2];
        
        UIButton * btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_2.frame = CGRectMake(0, 0, length_x, length_x + 45);
        [self.view_tuijian2 addSubview:btn_2];
        btn_2.tag = 252;
        [btn_2 addTarget:self action:@selector(btn_tuijianAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //3
        self.view_tuijian3 = [[UIView alloc] initWithFrame:CGRectMake(30 + length_x * 2, 5, length_x, length_x + 45)];
        self.view_tuijian3.backgroundColor = [UIColor whiteColor];
        [cell addSubview:self.view_tuijian3];
        
        UIImageView * image_3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, length_x, length_x)];
        [image_3 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"tuijianchanpin@2x.png"]];
        [self.view_tuijian3 addSubview:image_3];
        
        UILabel * name_3 = [[UILabel alloc] initWithFrame:CGRectMake(7, CGRectGetMaxY(image_3.frame) + 7, length_x - 8, 15)];
        name_3.text = @"产品名称_3";
        name_3.font = [UIFont systemFontOfSize:15];
        [self.view_tuijian3 addSubview:name_3];
        
        UILabel * price_3 = [[UILabel alloc] initWithFrame:CGRectMake(6, CGRectGetMaxY(name_3.frame) + 5, length_x - 6, 15)];
        price_3.text = @"￥39.9";
        price_3.textColor = [UIColor redColor];
        price_3.font = [UIFont systemFontOfSize:15];
        [self.view_tuijian3 addSubview:price_3];
        
        UIButton * btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_3.frame = CGRectMake(0, 0, length_x, length_x + 45);
        [self.view_tuijian3 addSubview:btn_3];
        btn_3.tag = 253;
        [btn_3 addTarget:self action:@selector(btn_tuijianAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        if(self.arr_tuijianData.count == 0)
        {
            self.view_tuijian1.hidden = YES;
            self.view_tuijian2.hidden = YES;
            self.view_tuijian3.hidden = YES;
        }
        else if(self.arr_tuijianData.count == 1)
        {
            self.view_tuijian2.hidden = YES;
            self.view_tuijian3.hidden = YES;
        }
        else if(self.arr_tuijianData.count == 2)
        {
            self.view_tuijian3.hidden = YES;
        }
    }
    else if(indexPath.row == 4)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 35, 15, 70, 20)];
        title.text = @"猜你喜欢";
        title.textAlignment = NSTextAlignmentCenter;
        [cell addSubview:title];
        
        UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40 - SCREEN_WIDTH / 6, 24, SCREEN_WIDTH / 6, 2)];
        view_line.backgroundColor = navi_bar_bg_color;
        [cell addSubview:view_line];
        
        UIView * view_lineR = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 + 40 , 24, SCREEN_WIDTH / 6, 2)];
        view_lineR.backgroundColor = navi_bar_bg_color;
        [cell addSubview:view_lineR];
    }
    else
    {
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self p_productList];
        
        [cell addSubview:self.collectionView_product];
    }
    
    return cell;
}

#pragma mark - 8大分类按钮
- (void )btn_fenleiAction:(UIButton *)sender
{
    NSInteger count = sender.tag - 150;
    NSLog(@"%ld",(long)count);
    
//    NSLog(@"跳到分类页面");
    
    ChanpinliebiaoViewController * chanpinliebiaoViewController = [[ChanpinliebiaoViewController alloc] init];
    [self showViewController:chanpinliebiaoViewController sender:nil];
}

#pragma mark - 限时抢购
- (void )btn_jianggouAction
{
//    NSLog(@"限时抢购");
    
    ChanpinxiangqingViewController * chanpinxiangqingViewController = [[ChanpinxiangqingViewController alloc] init];
    [self showViewController:chanpinxiangqingViewController sender:nil];
}

#pragma mark - 推荐
- (void )btn_tuijianAction:(UIButton *)sender
{
    NSInteger count = sender.tag - 250;
    NSLog(@"%ld",(long)count);
    
    ChanpinxiangqingViewController * chanpinxiangqingViewController = [[ChanpinxiangqingViewController alloc] init];
    [self showViewController:chanpinxiangqingViewController sender:nil];
}

#pragma mark - 猜你喜欢
- (void )p_productList
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    //每个item的大小
    int  item_length = (SCREEN_WIDTH - 30) / 2;
    layout.itemSize = CGSizeMake(item_length, item_length + 55);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView_product = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (item_length + 55 + 10) * 4 + 10) collectionViewLayout:layout];
    self.collectionView_product.delegate = self;
    self.collectionView_product.dataSource = self;
    self.collectionView_product.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.collectionView_product.scrollEnabled = NO;
    
    //注册
    [self.collectionView_product registerClass:[HomeProductCollectionViewCell class] forCellWithReuseIdentifier:@"cell_product"];
}

#pragma mark - classify_collectionView的代理
//有几个分区
- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个分区有多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr_cainixihuanData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeProductCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell_product" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    //
    [cell.image_product sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"cainixihuan@2x.png"]];
    
    return cell;
}

- (void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)indexPath.item);
    
    ChanpinxiangqingViewController * chanpinxiangqingViewController = [[ChanpinxiangqingViewController alloc] init];
    [self showViewController:chanpinxiangqingViewController sender:nil];
}

#pragma mark - 轮播图
- (void)p_lunbotu
{
    self.arr_lunboData = @[@"",@""].mutableCopy;
    
    if(SCREEN_WIDTH == 375)
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180)];
    }
    else if(SCREEN_WIDTH == 320)
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    }
    else
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    }
    self.lunbo_scrollView.pagingEnabled = YES;
    self.lunbo_scrollView.showsHorizontalScrollIndicator = NO;
    self.lunbo_scrollView.delegate = self;
    self.lunbo_scrollView.userInteractionEnabled = YES;
    //
    self.lunbo_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.arr_lunboData.count + 2, 0);
    
    if(self.arr_lunboData.count == 0)
    {
        self.lunbo_scrollView.scrollEnabled = NO;
        
        self.image1 = [[UIImageView alloc] init];
        self.image1.frame = CGRectMake(SCREEN_WIDTH * 0 , 0 , SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height);
        [self.image1 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"APP.png"]];
        [self.lunbo_scrollView addSubview:self.image1];
    }
    else
    {
        [self lunsdsd];
        
        self.lunbo_pageControl = [[UIPageControl alloc] init];
        
        if(SCREEN_WIDTH == 320)
        {
            self.lunbo_pageControl.frame = CGRectMake(0, self.lunbo_scrollView.frame.size.height - 20, SCREEN_WIDTH, 18);
        }
        else if(SCREEN_WIDTH == 375)
        {
            self.lunbo_pageControl.frame = CGRectMake(- 27.5, self.lunbo_scrollView.frame.size.height - 20, SCREEN_WIDTH, 18);
        }
        else
        {
            self.lunbo_pageControl.frame = CGRectMake(- 47, self.lunbo_scrollView.frame.size.height - 20, SCREEN_WIDTH, 18);
        }
        //    self.lunbo_pageControl.backgroundColor = [UIColor orangeColor];
        self.lunbo_pageControl.numberOfPages = self.arr_lunboData.count;
        self.lunbo_pageControl.currentPageIndicatorTintColor = navi_bar_bg_color;
        self.lunbo_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:202/255.0 green:218/255.0 blue:233/255.0 alpha:1];
    }
    //手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lunbotuAction:)];
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [self.lunbo_scrollView addGestureRecognizer:tapGesture];
}

#pragma mark - 轮播图的点击事件
- (void)lunbotuAction:(id)sender
{
    NSLog(@"%ld",(long)self.lunbo_pageControl.currentPage);
}

#pragma mark - 定时器的方法!
- (void)timerAction
{
    CGFloat x = self.lunbo_scrollView.contentOffset.x;
    int count = x / SCREEN_WIDTH;
    
    if(count < self.arr_lunboData.count)
    {
        count ++;
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = count - 1;
            
        }];
    }
    else if(count == self.arr_lunboData.count)
    {
        count ++;
        
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = 0;
            
        }];
    }
    else
    {
        count = 2;
        self.lunbo_scrollView.contentOffset = CGPointMake(1 * SCREEN_WIDTH, 0);
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = count - 1;
        }];
    }
}
#pragma mark - scrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x = self.lunbo_scrollView.contentOffset.x;
    
    int count = x / SCREEN_WIDTH;
    
    if(count == self.arr_lunboData.count + 1)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        self.lunbo_pageControl.currentPage = 0;
    }
    else if(count == 0)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(self.arr_lunboData.count * SCREEN_WIDTH, 0);
        self.lunbo_pageControl.currentPage = self.arr_lunboData.count - 1;
    }
    else
    {
        self.lunbo_pageControl.currentPage = count - 1;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if([self.lunbo_scrollView isEqual:scrollView])
    {
        [self.lunbo_timer invalidate];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if ([scrollView isEqual:self.lunbo_scrollView]) {
        
        self.lunbo_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
}

//
- (void)lunsdsd
{
    self.lunbo_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (self.arr_lunboData.count + 2), 0);
    
    self.lunbo_pageControl.numberOfPages = self.arr_lunboData.count;
    
    self.lunbo_scrollView.scrollEnabled = YES;
    
    for (int i = 1; i <= self.arr_lunboData.count; i++)
    {
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height)];
        
        [image sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"APP.png"]];
        
        [self.lunbo_scrollView addSubview:image];
    }
    
    self.image1 = [[UIImageView alloc] init];
    self.image1.frame = CGRectMake(SCREEN_WIDTH * 0 , 0 , SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height);
    [self.image1 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"APP.png"]];
    [self.lunbo_scrollView addSubview:self.image1];
    
    self.image2 = [[UIImageView alloc] init];
    self.image2.frame = CGRectMake(SCREEN_WIDTH * (self.arr_lunboData.count + 1), 0 , SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height);
    [self.image2 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"APP.png"]];
    [self.lunbo_scrollView addSubview:self.image2];
    
    if(self.isplay == 0)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        //轮播秒数
        self.lunbo_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        self.lunbo_pageControl.currentPage = 0;
        
        self.isplay = 1;
    }
    else
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * (self.lunbo_pageControl.currentPage + 1), 0);
    }
}

#pragma mark - 定时器（倒计时）
- (void)qianggou_timerAction
{
    NSDate * date1 = [NSDate date];
    NSDateFormatter * format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //当前时间
    NSString * str_1 = [format stringFromDate:date1];
    NSString * str_date = [str_1 substringToIndex:10];
    //最终时间
    NSString * str_2 = [NSString stringWithFormat:@"%@ 23:59:59",str_date];
    NSDate * date2 = [format dateFromString:str_2];
    NSTimeInterval x = [date2 timeIntervalSinceDate:date1];
    
    int t_1 = (x + 1) / 3600;
    int t_2 = ((x + 1) - t_1 * 3600) / 60;
    int t_3 = ((x + 1) - t_1 * 3600 - t_2 * 60);
    
    //
    self.time_3.text = [NSString stringWithFormat:@"%02d",t_3];
    self.time_2.text = [NSString stringWithFormat:@"%02d",t_2];
    self.time_1.text = [NSString stringWithFormat:@"%02d",t_1];
    
    if(t_1 <= 0 && t_2 <= 0 && t_3 <= 0)
    {
        self.time_1.hidden = YES;
        self.time_2.hidden = YES;
        self.time_3.hidden = YES;
        self.image_timedian1.hidden = YES;
        self.image_timedian2.hidden = YES;
        
        [self.timer invalidate];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)arr_lunboData
{
    if(_arr_lunboData == nil)
    {
        self.arr_lunboData = [NSMutableArray array];
    }
    return _arr_lunboData;
}

- (NSMutableArray *)arr_tuijianData
{
    if(_arr_tuijianData == nil)
    {
        self.arr_tuijianData = [NSMutableArray array];
    }
    return _arr_tuijianData;
}

- (NSMutableArray *)arr_cainixihuanData
{
    if(_arr_cainixihuanData == nil)
    {
        self.arr_cainixihuanData = [NSMutableArray array];
    }
    return _arr_cainixihuanData;
}

@end
